<template>
  <div class="my-container">
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="box1">
          <img
          :src="userInfo.photo"
          alt="">
          <div class="user-name">{{userInfo.name}}</div>
          <div class="edit"
            @click="$router.push('/user/profile')"
          >编辑资料</div>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <div class="item-title">头条</div>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <div class="item-title">关注</div>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <div class="item-title">粉丝</div>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <div class="item-title">获赞</div>
        </div>
      </div>
    </div>
    <div class="header not-login" v-else >
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="@/assets/mobile.png" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <van-grid :column-num="2" class="nav-grid" clickable>
      <van-grid-item class="grid-item">
        <van-icon class="my-grid-icon1" slot="icon" name="star-o" />
        <span
          class="text"
          slot="text"
          @click="$router.push('/myCollection')"
        >收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <van-icon class="my-grid-icon2" slot="icon" name="clock-o" />
        <span
          class="text"
          slot="text"
          @click="$router.push('/myCollection')"
        >历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell-group class="cell-m">
      <van-cell class="message" title="消息通知" is-link />
      <van-cell class="zhi" title="小智同学" is-link />
    </van-cell-group>
    <div
    class="quit"
    v-if="user"
    @click="handleQuitLogin"
    >退出登录</div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog } from 'vant'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'MyIndex',
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  methods: {
    handleQuitLogin () {
      Dialog.confirm({
        title: '确认退出？',
        confirmButtonColor: '#3e9cfa'
      }).then(() => {
        // 确认退出后要清除容器和本地存储中的登录状态
        // 因为本地存储中的数据也是在setuser中赋值的 所以我们只需修改容器就可以
        // 容器中的数据变化，其对应的视图也会变化
        this.$store.commit('setUser', null)
      }).catch(() => {
      })
    },
    async loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        this.userInfo = data.data
      } catch (err) {
        this.$toast('登录过期，请重新登录')
      }
    }
  }
}
</script>

<style lang="less">
  .header {
    height: 361px;
    /* background: url('../../assets/banner.png'); */
    background: url('~@/assets/banner.png');
    /* @指向的是src，在css中要加~，js中可以直接用@ */
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      align-items: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      box-sizing: border-box;
      overflow: hidden;
      height: 231px;
      .box1 {
        box-sizing: border-box;
        margin: 75px 30px 0 30px;
        display: flex;
        align-items: center;
        height: 140px;
        img {
          width: 136px;
          height: 136px;
          border: 4px solid #fff;
          border-radius: 68px;
        }
        .user-name {
          flex: 1;
          font-size: 30px;
          color: #fff;
          margin-left: 25px;
        }
        .edit {
          width: 116px;
          line-height: 34px;
          text-align: center;
          font-size: 20px;
          color: #666;
          background-color: #fff;
          border-radius: 20px;
        }
      }
    }
    .data-stats {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 130px;
      color: #fff;
      .data-item {
        flex: 1;
        text-align: center;
        .count {
          font-size: 36px;
        }
        .item-title {
          font-size: 23px;
        }
      }
    }
  }
  .nav-grid {
    margin-bottom: 8px;
    .grid-item {
      height: 141px;
      .my-grid-icon1 {
        font-size: 45px;
        color: #eb5253;
      }
      .my-grid-icon2 {
        font-size: 45px;
        color: #ff9d1d;
      }
      .text {
        font-size: 28px;
        color: #333;
      }
    }
  }
  div.cell-m {
    margin-bottom: 8px;
    .message, .zhi {
      height: 100px;
    }
    .van-cell::after {
      border: 1px solid #edeff3;
    }
  }
  .quit {
    line-height: 102px;
    text-align: center;
    font-size: 30px;
    color: #d86262;
    background-color: #fff;
  }
</style>
